import { BufferAttribute } from 'three'
import { Mesh, MeshStandardMaterial, BufferGeometry, Color } from 'three'
import { pictureTexture } from './Ttextures'
import { VertexNormalsHelper } from 'three/examples/jsm/helpers/VertexNormalsHelper'

export const codeModelList = []

const size = 10

const points = new Float32Array([
  // up
  -size,
  size,
  size,
  size,
  size,
  size,
  size,
  size,
  -size,
  -size,
  size,
  -size,

  // down
  size,
  -size,
  size,
  -size,
  -size,
  size,
  -size,
  -size,
  -size,
  size,
  -size,
  -size,

  // arrow
  -size,
  -size,
  size,
  size,
  -size,
  size,
  size,
  size,
  size,
  -size,
  size,
  size,

  // behind
  size,
  -size,
  -size,
  -size,
  -size,
  -size,
  -size,
  size,
  -size,
  size,
  size,
  -size,

  // left
  -size,
  -size,
  -size,
  -size,
  -size,
  size,
  -size,
  size,
  size,
  -size,
  size,
  -size,

  // right
  size,
  -size,
  size,
  size,
  -size,
  -size,
  size,
  size,
  -size,
  size,
  size,
  size,
])
const normals = new Float32Array([
  // up
  0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,

  // down
  0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,

  // arrow
  0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,

  // behind
  0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1,

  // left
  -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0,

  // right
  1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
])
// 规定逆时针为正面，默认渲染正面
const index = [
  // 上
  0, 1, 2, 2, 3, 0,
  // 下
  4, 5, 6, 6, 7, 4,
  // 前
  8, 9, 10, 10, 11, 8,
  // 后
  12, 13, 14, 14, 15, 12,
  // 左
  16, 17, 18, 18, 19, 16,
  // 右
  20, 21, 22, 22, 23, 20,
]
const uv = new Float32Array([
  0, 0, 1, 0, 1, 1, 0, 1,

  0, 0, 1, 0, 1, 1, 0, 1,

  0, 0, 1, 0, 1, 1, 0, 1,

  0, 0, 1, 0, 1, 1, 0, 1,

  0, 0, 1, 0, 1, 1, 0, 1,

  0, 0, 1, 0, 1, 1, 0, 1,
]) //uv属性，贴图顶点映射

const gemoetry = new BufferGeometry()
gemoetry.setAttribute('position', new BufferAttribute(points, 3)) //顶点信息
gemoetry.setAttribute('normal', new BufferAttribute(normals, 3)) //法线信息
gemoetry.setAttribute('uv', new BufferAttribute(uv, 2)) //纹理坐标信息
gemoetry.setIndex(index)

const material = new MeshStandardMaterial({
  color: 'white',
  map: pictureTexture,
})
export const codeBox = new Mesh(gemoetry, material)
codeBox.position.y = 35
codeBox.position.x = -30
// 更新世界矩阵和本地矩阵，聚光灯辅助线是根据目标的矩阵来确定的。
// 目标(物体)的坐标改变需要更新矩阵才能使辅助线跟随
codeBox.updateMatrix()
codeBox.updateMatrixWorld()
codeBox.castShadow = true

const boxNormalHelper = new VertexNormalsHelper(
  codeBox,
  10,
  new Color('blue').getHex()
)

codeModelList.push(codeBox, boxNormalHelper)
